package com.share.domain.strategy.service.rule.tree.impl;

import com.share.domain.strategy.model.vo.RuleLogicCheckTypeVo;
import com.share.domain.strategy.service.rule.tree.ILogicTreeNode;
import com.share.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import com.share.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 兜底奖励树节点
 */
@Component("rule_luck_award")
@Slf4j
public class RuleLuckAwardLogicTreeNode implements ILogicTreeNode {
    @Override
    public DefaultTreeFactory.TreeActionEntity logic(String userId, Long strategyId, Integer awardId, String ruleValue, Date endDateTime) {
        log.info("规则过滤-兜底奖励：userId{}，strategyId：{}，awardId：{}",userId,strategyId,awardId);
        //101:1,100
        //101:--1,100
        String[] split = ruleValue.split(Constants.COLON);
        if(split.length==0){
            log.error("规则过滤-兜底奖励,兜底奖励未配置警告：userId{}，strategyId：{}，awardId：{}",userId,strategyId,awardId);
            throw new RuntimeException("兜底奖励未配置"+ruleValue);
        }
        Integer luckAwardId = Integer.valueOf(split[0]);
        String awardRuleValue = split.length > 1 ? split[1] : "";
        log.info("规则过滤-兜底奖励 userId{}，strategyId：{}，awardId：{}，awardRuleValue:{}",userId,strategyId,luckAwardId,awardRuleValue);


        return DefaultTreeFactory.TreeActionEntity.builder()
                .ruleLogicCheckType(RuleLogicCheckTypeVo.TAKE_OVER)
                .strategyAwardVo(DefaultTreeFactory.StrategyAwardVo.builder()
                        .awardId(awardId)
                        .awardRuleValue(awardRuleValue)
                        .build())
                .build();

    }
}
